<?php

namespace App\Services;

use App\Models\Department;
use App\Models\DepartmentFile;
use App\Models\DepartmentFushu;
use App\Models\DepartmentStaff;
use Exception;

class DepartmentService
{
    public function downloadDwAllFiles(Department $department)
    {
        $count = 0;
        $department_file = DepartmentFile::query()->where('dwdm', $department->dwdm)->first();

        $zip = new \ZipArchive();
        # zip包名称
        $zippath = $department->dwmc . date("YmdHis"). ".zip";
        $zip->open(file_local_private_path($zippath), \ZipArchive::CREATE|\ZipArchive::OVERWRITE);
        if (!$zip) {
            exit('创建压缩包失败');
        }
        if ($department_file) {
            if ($department_file->ppt_filepath && file_exists(file_local_path($department_file->ppt_filepath))) {
                $basename = get_basename($department_file->ppt_filepath);
                $to_dir = $department->dwmc . '/' . $basename;
                $zip->addFile(file_local_path($department_file->ppt_filepath), $to_dir);
                $count++;
            }
            if ($department_file->pdf_filepath && file_exists(file_local_path($department_file->pdf_filepath))) {
                $basename = get_basename($department_file->pdf_filepath);
                $to_dir = $department->dwmc . '/' . $basename;
                $zip->addFile(file_local_path($department_file->pdf_filepath), $to_dir);
                $count++;
            }
        }
        # 判断附属单位材料是否已经上传完成
        $fushus = DepartmentFushu::getFushuByDwdm($department->dwdm);
        foreach($fushus as $fushu) {
            if (($fushu->filepath && file_exists(file_local_path($fushu->filepath)))) {
                $basename = get_basename($fushu->filepath);
                $to_dir = $department->dwmc . '/' . $basename;
                $zip->addFile(file_local_path($fushu->filepath), $to_dir);
                $count++;
            }
        }
        # 判断个人材料是否已经上传完成
        $personals = DepartmentStaff::getStaffsByDwdm($department->dwdm);
        foreach($personals as $personal) {
            if (($personal->filepath && file_exists(file_local_path($personal->filepath)))) {
                $basename = get_basename($personal->filepath);
                $to_dir = $department->dwmc . '/' . $basename;
                $zip->addFile(file_local_path($personal->filepath), $to_dir);
                $count++;
            }
        }

        if ($count == 0) {
            exit('暂无材料');
        } else {
            $zip->close();
        }
        return $zippath;
    }

    public function downloadPPtx($type)
    {
        $count = 0;
        $title = $type == Department::TYPE_BZ ? '2023年中层领导班子和领导人员述职评审材料' : '2023年院级党组织书记抓基层党建述职评议测评材料';
        $departments = Department::query()->where('type', $type)->get();
        $zip = new \ZipArchive();
        # zip包名称
        $zippath = $title.date("YmdHis"). ".zip";
        $zip->open(file_local_private_path($zippath), \ZipArchive::CREATE|\ZipArchive::OVERWRITE);
        foreach($departments as $department) {
            $department_file = DepartmentFile::query()->where('dwdm', $department->dwdm)->first();
            if (!$zip) {
                exit('创建压缩包失败');
            }
            if ($department_file) {
                if ($department_file->ppt_filepath && file_exists(file_local_path($department_file->ppt_filepath))) {
                    $count++;
                    $basename = get_basename($department_file->ppt_filepath);
                    $to_dir = $title . '/'. $basename;
                    $zip->addFile(file_local_path($department_file->ppt_filepath), $to_dir);
                }
            }
        }
        if ($count == 0) {
            exit('暂无材料');
        } else {
            $zip->close();
        }
        return $zippath;
    }

    public function downloadAllFiles($type)
    {
        $count = 0;
        $title = $type == Department::TYPE_BZ ? '2023年中层领导班子和领导人员述职评审材料' : '2023年院级党组织书记抓基层党建述职评议测评材料';
        $departments = Department::query()->where('type', $type)->get();
        $zip = new \ZipArchive();
        # zip包名称
        $zippath = $title.date("YmdHis"). ".zip";
        $zip->open(file_local_private_path($zippath), \ZipArchive::CREATE|\ZipArchive::OVERWRITE);
        if (!$zip) {
            exit('创建压缩包失败');
        }
        foreach($departments as $department) {
            $department_file = DepartmentFile::query()->where('dwdm', $department->dwdm)->first();
            if ($department_file) {
                if ($department_file->ppt_filepath && file_exists(file_local_path($department_file->ppt_filepath))) {
                    $basename = get_basename($department_file->ppt_filepath);
                    $to_dir = $department->dwmc . '/' . $basename;
                    $zip->addFile(file_local_path($department_file->ppt_filepath), $to_dir);
                    $count++;
                }
                if ($department_file->pdf_filepath && file_exists(file_local_path($department_file->pdf_filepath))) {
                    $basename = get_basename($department_file->pdf_filepath);
                    $to_dir = $department->dwmc . '/' . $basename;
                    $zip->addFile(file_local_path($department_file->pdf_filepath), $to_dir);
                    $count++;
                }
            }
            $fushus = DepartmentFushu::getFushuByDwdm($department->dwdm);
            foreach($fushus as $fushu) {
                if (($fushu->filepath && file_exists(file_local_path($fushu->filepath)))) {
                    $basename = get_basename($fushu->filepath);
                    $to_dir = $department->dwmc . '/' . $basename;
                    $zip->addFile(file_local_path($fushu->filepath), $to_dir);
                    $count++;
                }
            }
            $personals = DepartmentStaff::getStaffsByDwdm($department->dwdm);
            foreach($personals as $personal) {
                if (($personal->filepath && file_exists(file_local_path($personal->filepath)))) {
                    $basename = get_basename($personal->filepath);
                    $to_dir = $department->dwmc . '/' . $basename;
                    $zip->addFile(file_local_path($personal->filepath), $to_dir);
                    $count++;
                }
            }
        }
        if ($count == 0) {
            exit('暂无材料');
        } else {
            $zip->close();
        }
        return $zippath;
    }
}